package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.cart.vo.AddCartSuccessVo;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.cart.CartFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author:ahang
 * @create:2022-10-19 14:25
 * @Description:
 */
@Controller
public class CartController {

    @Autowired
    CartFeignClient cartFeignClient;


    /**
     * 添加购物车
     * @param skuId
     * @param skuNum
     * @param model
     * @return
     */
    @GetMapping("/addCart.html")
    public String addCart(@RequestParam("skuId") Long skuId,
                          @RequestParam("skuNum") Integer skuNum,
                          Model model) {

        //把商品添加到购物车
        Result<AddCartSuccessVo> result = cartFeignClient.addToCart(skuId, skuNum);

        if(result.isOk()){
            model.addAttribute("skuInfo",result.getData().getSkuInfo());
            model.addAttribute("skuNum",result.getData().getSkuNum());
        }else{
            model.addAttribute("msg",result.getMessage());
            return "cart/error";
        }

        return "cart/addCart";
    }

    /**
     * 购物车列表页
     * @return
     */
    @GetMapping("cart.html")
    public String cartList(){
        return "cart/index.html";
    }

    /**
     * 删除选中的商品
     * @return
     */
    @GetMapping("/cart/deleteChecked")
    public String deleteChecked(){
        cartFeignClient.deleteChecked();
        return "redirect:http://cart.gmall.com/cart.html";
    }
}
